<template>
  <div class="artists-container">
    <div class="banner">
      <div class="banner-content">
        <h1>京剧艺术家</h1>
        <p>传承戏曲艺术，弘扬国粹精神</p>
      </div>
    </div>
    
    <div class="introduction-section">
      <div class="intro-content">
        <h2>京剧表演艺术</h2>
        <p>京剧表演艺术以"唱、念、做、打"为基本功，集声腔、表演、舞蹈、武术、音乐和美术为一体。艺术家们通过多年的刻苦训练，形成了独特的艺术风格和表演特点，使京剧艺术绽放异彩。</p>
      </div>
    </div>
    
    <div class="filter-section">
      <div class="search-bar">
        <el-input 
          v-model="searchQuery" 
          placeholder="搜索艺术家姓名" 
          prefix-icon="el-icon-search" 
          clearable
          @keyup.enter.native="handleSearch"
          @clear="handleSearch">
          <el-button slot="append" icon="el-icon-search" @click="handleSearch"></el-button>
        </el-input>
      </div>
      
      <div class="filter-options">
        <div class="filter-item">
          <span class="filter-label">行当:</span>
          <el-select v-model="categoryFilter" placeholder="全部行当" @change="handleFilterChange" clearable>
            <el-option 
              v-for="category in categories" 
              :key="category.categoriesId" 
              :label="category.categoriesName" 
              :value="category.categoriesId">
            </el-option>
          </el-select>
        </div>
        
        <div class="filter-item">
          <span class="filter-label">流派:</span>
          <el-select v-model="schoolFilter" placeholder="全部流派" @change="handleFilterChange" clearable>
            <el-option 
              v-for="school in schools" 
              :key="school.schoolId" 
              :label="school.schoolName" 
              :value="school.schoolId">
            </el-option>
          </el-select>
        </div>
      </div>
    </div>
    
    <div class="content-section">
      <div v-if="loading" class="loading-container">
        <el-skeleton :rows="3" animated />
        <el-skeleton :rows="3" animated />
        <el-skeleton :rows="3" animated />
        <el-skeleton :rows="3" animated />
      </div>
    
      <div v-else class="artist-card-grid">
        <el-card 
          v-for="artist in filteredArtists" 
          :key="artist.artistId" 
          class="artist-card" 
          shadow="hover"
          @click.native="viewArtistDetail(artist.artistId)">
          <div class="artist-header">
            <h3 class="artist-name">{{ artist.artistName }}</h3>
            <div class="artist-tags">
              <template v-if="artist.categoryId">
                <el-tag size="small" type="danger" v-for="(catId, index) in artist.categoryId.split(',')" :key="'cat-'+index" class="category-tag">{{ getCategoryNameById(catId) }}</el-tag>
              </template>
              <el-tag size="small" type="warning" v-if="artist.school" class="category-tag">{{ artist.school }}</el-tag>
            </div>
          </div>
          <div class="artist-content">
            <div class="artist-avatar">
              <img :src="artist.avatar || require('@/assets/history/opera_mlf.jpg')" :alt="artist.artistName">
            </div>
            <div class="artist-info">
              <p class="artist-description">{{ limitText(artist.description, 100) }}</p>
            </div>
          </div>
          <div class="artist-footer">
            <el-button type="text">查看详情</el-button>
          </div>
        </el-card>
      </div>
      
      <div class="pagination-container" v-if="totalArtists > 0">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[8, 16, 24, 32]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="totalArtists">
        </el-pagination>
      </div>
      
      <div class="no-data" v-if="filteredArtists.length === 0 && !loading">
        <img src="@/assets/opera_logo.png" alt="没有数据">
        <p>暂无符合条件的艺术家</p>
      </div>
    </div>
  </div>
</template>

<script>
import service from '@/utils/request';

export default {
  name: 'ArtistsVue',
  data() {
    return {
      loading: false,
      searchQuery: '',
      categoryFilter: '',
      schoolFilter: '',
      currentPage: 1,
      pageSize: 16,
      totalArtists: 0,
      artists: [],
      
      // 行当和流派数据
      categories: [],
      schools: [],
    }
  },
  computed: {
    filteredArtists() {
      return this.artists;
    },
    
    // 获取行当名称的映射对象
    categoryMap() {
      const map = {};
      this.categories.forEach(cat => {
        map[cat.categoriesId] = cat.categoriesName;
      });
      return map;
    },
    
    // 获取流派名称的映射对象
    schoolMap() {
      const map = {};
      this.schools.forEach(school => {
        map[school.schoolId] = school.schoolName;
      });
      return map;
    }
  },
  mounted() {
    document.title = '京剧苑 - 京剧艺术家';
    window.scrollTo(0, 0);
    this.initData();
    this.queryParams = this.$route.query;
    if (this.queryParams.categoryId) {
      this.categoryFilter = this.queryParams.categoryId;
    }
  },
  methods: {
    // 初始化页面数据
    initData() {
      // 并行加载所有数据，提高加载速度
      this.loading = true;
      Promise.all([
        this.fetchCategories(),
        this.fetchSchools()
      ]).then(() => {
        this.fetchArtists();
      }).catch(() => {
        this.loading = false;
        this.$message.error('初始化数据失败，请刷新页面重试');
      });
    },
    
    fetchCategories() {
      // 从后端获取行当数据
      return new Promise((resolve) => {
        service.get('/api/content/category/getCategoryList', {
          params: {
            pageNum: 1,
            pageSize: 100
          }
        }).then(res => {
          if (res.code === 200) {
            this.categories = res.data.list;
          } else {
            this.$message.error('获取行当列表失败');
          }
          resolve();
        }).catch(error => {
          this.$message.error('获取行当列表失败：' + (error.message || '未知错误'));
          resolve();
        });
      });
    },
    
    fetchSchools() {
      // 从后端获取流派数据
      return new Promise((resolve) => {
        service.get('/api/content/school/getSchoolList', {
          params: {
            pageNum: 1,
            pageSize: 100
          }
        }).then(res => {
          if (res.code === 200) {
            this.schools = res.data.list;
          } else {
            this.$message.error('获取流派列表失败');
          }
          resolve();
        }).catch(error => {
          this.$message.error('获取流派列表失败：' + (error.message || '未知错误'));
          resolve();
        });
      });
    },
    
    fetchArtists() {
      // 构建请求参数
      const params = {
        pageNum: this.currentPage,
        pageSize: this.pageSize,
        artistName: this.searchQuery,
        categoryId: this.categoryFilter,
        schoolId: this.schoolFilter
      };
      
      // 调用后端API获取艺术家数据
      service.get('/api/content/artist/getArtistList', { params }).then(res => {
        if (res.code === 200) {
          this.artists = res.data.list;
          this.totalArtists = res.data.total;
        } else {
          this.$message.error('获取艺术家列表失败');
        }
        this.loading = false;
      }).catch(error => {
        this.$message.error('获取艺术家列表失败：' + (error.message || '未知错误'));
        this.loading = false;
      });
    },
    
    handleSearch() {
      this.currentPage = 1;
      this.fetchArtists();
    },
    
    handleFilterChange() {
      this.currentPage = 1;
      this.fetchArtists();
    },
    
    handleSizeChange(size) {
      this.pageSize = size;
      this.fetchArtists();
    },
    
    handleCurrentChange(page) {
      this.currentPage = page;
      this.fetchArtists();
    },
    
    viewArtistDetail(artistId) {
      if (!artistId) return;
      
      // 添加加载状态，提升用户体验
      const loading = this.$loading({
        lock: true,
        text: '正在加载艺术家详情...',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      });
      
      // 延迟50ms后跳转，让loading效果显示出来
      setTimeout(() => {
        loading.close();
        this.$router.push({ name: 'ArtistDetail', params: { id: artistId }});
      }, 50);
    },
    
    limitText(text, length) {
      if (!text) return '';
      return text.length > length ? text.substring(0, length) + '...' : text;
    },
    
    getCategoryNameById(catId) {
      return this.categoryMap[catId] || '未知行当';
    }
  }
}
</script>

<style scoped>
.artists-container {
  min-height: 100vh;
  padding-bottom: 50px;
  background-color: #f9f5f0;
}

.banner {
  height: 500px;
  background-image: url('../assets/opera_artist.png');
  background-size: cover;
  background-position: center;
  position: relative;
  margin-bottom: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.banner:before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(to bottom, rgba(139, 0, 0, 0.7), rgba(0, 0, 0, 0.5));
}

.banner-content {
  position: relative;
  z-index: 1;
  text-align: center;
  color: white;
}

.banner-content h1 {
  font-size: 4rem;
  font-family: 'STXingkai', 'STZhongsong', serif;
  margin-bottom: 15px;
  text-shadow: 2px 2px 8px rgba(0, 0, 0, 0.6);
  color: #FFD700;
  letter-spacing: 10px;
}

.banner-content p {
  font-size: 1.4rem;
  max-width: 600px;
  margin: 0 auto;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.6);
  letter-spacing: 3px;
}

.introduction-section {
  padding: 60px 10%;
  background-color: #fff;
  text-align: center;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  margin-bottom: 40px;
}

.intro-content {
  max-width: 900px;
  margin: 0 auto;
}

.intro-content h2 {
  font-size: 2.4rem;
  color: #8B0000;
  margin-bottom: 30px;
  font-family: 'STZhongsong', serif;
  position: relative;
  display: inline-block;
}

.intro-content h2::after {
  content: '';
  position: absolute;
  bottom: -10px;
  left: 50%;
  transform: translateX(-50%);
  width: 80px;
  height: 3px;
  background-color: #8B0000;
}

.intro-content p {
  font-size: 1.2rem;
  line-height: 2;
  color: #333;
  margin-bottom: 20px;
  text-align: justify;
}

.filter-section {
  padding: 20px 5%;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: center;
  background-color: #fff;
  margin-bottom: 30px;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
  max-width: 1200px;
  margin-left: auto;
  margin-right: auto;
}

.search-bar {
  width: 300px;
  max-width: 100%;
  margin-right: 20px;
}

.filter-options {
  display: flex;
  flex-wrap: wrap;
  gap: 20px;
}

.filter-item {
  display: flex;
  align-items: center;
}

.filter-label {
  margin-right: 10px;
  font-weight: bold;
  color: #666;
}

.content-section {
  padding: 30px 8%;
  max-width: 1200px;
  margin: 0 auto;
}

.loading-container {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 30px;
  margin-bottom: 40px;
  padding: 20px 0;
}

.artist-card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 30px;
  margin-bottom: 40px;
}

.artist-card {
  cursor: pointer;
  transition: all 0.3s ease;
  height: 100%;
  border: none;
  border-radius: 8px;
  overflow: hidden;
  background-color: rgba(255, 255, 255, 0.95);
}

.artist-card:hover {
  transform: translateY(-8px);
  box-shadow: 0 15px 30px rgba(139, 0, 0, 0.15);
}

.artist-header {
  margin-bottom: 20px;
  border-bottom: 1px solid #f0f0f0;
  padding-bottom: 15px;
}

.artist-header h3 {
  font-size: 1.8rem;
  color: #8B0000;
  margin: 0 0 12px;
  font-family: 'STZhongsong', serif;
}

.artist-content {
  display: flex;
  margin-bottom: 20px;
}

.artist-avatar {
  width: 100px;
  height: 120px;
  border-radius: 8px;
  overflow: hidden;
  margin-right: 15px;
  border: 1px solid #e0e0e0;
  flex-shrink: 0;
}

.artist-avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.5s;
}

.artist-card:hover .artist-avatar img {
  transform: scale(1.05);
}

.artist-info {
  flex: 1;
}

.artist-tags {
  margin-bottom: 15px;
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.artist-tags .el-tag {
  margin-right: 0;
  margin-bottom: 0;
  padding: 0 8px;
  font-size: 0.8rem;
  border-radius: 4px;
  height: 22px;
  line-height: 22px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 46px;
  text-align: center;
}

.category-tag {
  white-space: nowrap;
}

.artist-description {
  color: #555;
  margin-bottom: 15px;
  line-height: 1.7;
  font-size: 1rem;
}

.artist-footer {
  text-align: right;
  padding-top: 10px;
  border-top: 1px solid #f0f0f0;
}

.artist-footer .el-button {
  color: #8B0000;
  font-weight: bold;
}

.pagination-container {
  display: flex;
  justify-content: center;
  margin-top: 20px;
  padding: 10px 0;
}

.no-data {
  text-align: center;
  padding: 50px 0;
  color: #999;
  background: #f9f9f9;
  border-radius: 8px;
  margin: 20px 0;
}

.no-data img {
  width: 150px;
  margin-bottom: 20px;
  opacity: 0.5;
}

@media (max-width: 1200px) {
  .artist-card-grid, .loading-container {
    grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
    gap: 20px;
  }
  
  .filter-section {
    flex-direction: column;
    align-items: flex-start;
  }
  
  .search-bar {
    width: 100%;
    margin-bottom: 15px;
    margin-right: 0;
  }
  
  .filter-options {
    width: 100%;
  }
}

@media (max-width: 768px) {
  .banner-content h1 {
    font-size: 2.5rem;
    letter-spacing: 5px;
  }
  
  .banner-content p {
    font-size: 1.2rem;
    letter-spacing: 1px;
  }
  
  .banner {
    height: 240px;
  }
  
  .artist-card-grid, .loading-container {
    grid-template-columns: repeat(auto-fill, minmax(100%, 1fr));
  }
  
  .filter-options {
    flex-direction: column;
    gap: 10px;
  }
  
  .filter-item {
    width: 100%;
  }
  
  .filter-label {
    min-width: 60px;
  }
  
  .content-section {
    padding: 20px 3%;
  }
  
  .intro-content h2 {
    font-size: 2rem;
  }
  
  .artist-header h3 {
    font-size: 1.5rem;
  }
  
  .introduction-section {
    padding: 40px 5%;
  }
}

@media (max-width: 480px) {
  .banner-content h1 {
    font-size: 2rem;
    letter-spacing: 3px;
  }
  
  .banner-content p {
    font-size: 1rem;
  }
  
  .artist-content {
    flex-direction: column;
  }
  
  .artist-avatar {
    width: 100%;
    height: 180px;
    margin-right: 0;
    margin-bottom: 15px;
  }
}
</style> 